// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <pollux/dwio/dwrf/writer/flush_policy.h>

namespace {
static constexpr size_t kNumDictioanryTestsPerStripe = 3UL;
} // namespace

namespace kumo::pollux::dwrf {

DefaultFlushPolicy::DefaultFlushPolicy(
    uint64_t stripeSizeThreshold,
    uint64_t dictionarySizeThreshold)
    : stripeSizeThreshold_{stripeSizeThreshold},
      dictionarySizeThreshold_{dictionarySizeThreshold},
      dictionaryAssessmentThreshold_{getDictionaryAssessmentIncrement()} {}

uint64_t DefaultFlushPolicy::getDictionaryAssessmentIncrement() const {
  return stripeSizeThreshold_ / kNumDictioanryTestsPerStripe;
}

FlushDecision DefaultFlushPolicy::shouldFlushDictionary(
    bool flushStripe,
    bool overMemoryBudget,
    const dwio::common::StripeProgress& stripeProgress,
    int64_t dictionaryMemoryUsage) {
  if (flushStripe) {
    return FlushDecision::SKIP;
  }

  if (dictionaryMemoryUsage > dictionarySizeThreshold_) {
    return FlushDecision::FLUSH_DICTIONARY;
  }
  if (stripeProgress.stripeSizeEstimate >= dictionaryAssessmentThreshold_) {
    // In the current implementation, since we don't ever change encoding
    // decision after the first stripe, we don't need to ever reset this
    // threshold.
    dictionaryAssessmentThreshold_ +=
        stripeSizeThreshold_ / kNumDictioanryTestsPerStripe;
    return FlushDecision::EVALUATE_DICTIONARY;
  }
  return FlushDecision::SKIP;
}

FlushDecision DefaultFlushPolicy::shouldFlushDictionary(
    bool flushStripe,
    bool overMemoryBudget,
    const dwio::common::StripeProgress& stripeProgress,
    const WriterContext& context) {
  return shouldFlushDictionary(
      flushStripe,
      overMemoryBudget,
      stripeProgress,
      context.getMemoryUsage(MemoryUsageCategory::DICTIONARY));
}

RowsPerStripeFlushPolicy::RowsPerStripeFlushPolicy(
    std::vector<uint64_t> rowsPerStripe)
    : rowsPerStripe_{std::move(rowsPerStripe)} {
  // Note: Vector will be empty for empty files.
  for (auto i = 0; i < rowsPerStripe_.size(); i++) {
    DWIO_ENSURE_GT(
        rowsPerStripe_.at(i),
        0,
        "More than 0 rows expected in the stripe at ",
        i,
        melon::join(",", rowsPerStripe_));
  }
}

// We can throw if writer reported the incoming write to be over memory budget.
bool RowsPerStripeFlushPolicy::shouldFlush(
    const dwio::common::StripeProgress& stripeProgress) {
  const auto stripeIndex = stripeProgress.stripeIndex;
  const auto stripeRowCount = stripeProgress.stripeRowCount;
  DWIO_ENSURE_LT(
      stripeIndex,
      rowsPerStripe_.size(),
      "Stripe index is bigger than expected");

  DWIO_ENSURE_LE(
      stripeRowCount,
      rowsPerStripe_.at(stripeIndex),
      "More rows in Stripe than expected ",
      stripeIndex);

  if ((stripeIndex + 1) == rowsPerStripe_.size()) {
    // Last Stripe is always flushed at the time of close.
    return false;
  }

  return stripeRowCount == rowsPerStripe_.at(stripeIndex);
}
} // namespace kumo::pollux::dwrf
